package com.jae.service.impl;


import com.jae.mapper.AdminUserMapper;
import com.jae.mapper.UserMapper;
import com.jae.mapper.UserRoleMapper;
import com.jae.model.adminUser.AdminUser;
import com.jae.model.adminUser.AdminUserDTO;
import com.jae.model.user.User;
import com.jae.model.userRole.UserRole;
import com.jae.service.AdminUserService;
import com.jae.utils.Assert;
import com.jae.utils.PasswordUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class AdminUserServiceImpl extends BaseServiceImpl implements AdminUserService {

	@Resource
	private AdminUserMapper adminUserMapper;
	@Resource
	private UserMapper userMapper;
	@Resource
	private UserRoleMapper userRoleMapper;
	

	/**
	* 	获取所有后台管理员	
	*/
	@Override
	public List<AdminUserDTO> findAll(Map map) {
		return adminUserMapper.findAll(map);
	}
	
	/**
	* 	获取一条后台管理员	
	*/
	@Override
	public AdminUserDTO findOne(Map map) {
		return adminUserMapper.findOne(map);
	}
	
	/**
	* 	根据ID获取后台管理员	
	*/
	@Override
	public AdminUserDTO findById(Integer user_id) {
		AdminUser adminUser = new AdminUser();
		adminUser.setUser_id(user_id);
		return adminUserMapper.findById(adminUser);
	}
	
	
	
	/**
	 * 新增后台管理员
	 */
	@Override
	public boolean insert(AdminUser adminUser,String role_ids, User user){
		String username = adminUser.getUsername();
		String password = adminUser.getPassword();
		Assert.hasText(role_ids, "角色不能为空");
		
		Assert.hasText(username, "用户名不能为空");
		Assert.hasText(password, "密码不能为空");

		Map map = new HashMap();
		map.put("username",username);
		if(userMapper.findByUsername(map) != null)
			serviceError("用户名已存在");
		
		User newUser = new User();
		newUser.setUsername(username);
		newUser.setPassword(PasswordUtils.encode(password));
		newUser.setCreated_time(new Date());
		if(userMapper.insert(newUser)!=1)
			serviceError("添加管理员失败");
		
		adminUser.setPassword(PasswordUtils.encode(password));
		adminUser.setUser_id(newUser.getUser_id());
		if(adminUserMapper.insert(adminUser)!=1)
			serviceError("插入失败");
		
		String[] strs = role_ids.split(";");
		List<UserRole> list = new ArrayList<UserRole>();
		for (String s : strs) {
			UserRole ur = new UserRole();
			ur.setUser_id(user.getUser_id());
			ur.setRole_id(Integer.parseInt(s));
			list.add(ur);
		}
		
		if(userRoleMapper.insertBatch(list)!=list.size())
			serviceError("添加管理员失败");
		
		return true;
		
	}
	
	/**
	 * 修改后台管理员
	 */
	@Override
	public boolean update(AdminUser adminUser, User user){
		if(adminUserMapper.update(adminUser)!=1)
			serviceError("修改失败");
		return true;
	}
	
	
	/**
	 * 	逻辑删除后台管理员
	 */
	@Override
	public boolean updateDelete(Integer user_id, User user){
		AdminUser old=new AdminUser();
		//adminUser.setLast_updated_by(user.getUser_id());
		//adminUser.setLast_updated_date(new Date());
		old.setUser_id(user_id);
		
		if(adminUserMapper.updateDelete(old)!=1)
			serviceError("逻辑删除失败");
		return true;
	}
	
	/**
	 * 	物理删除后台管理员
	 */
	@Override
	public boolean delete(Integer user_id){
		AdminUser old = new AdminUser();
		old.setUser_id(user_id);
		if(adminUserMapper.delete(old)!=1)
			serviceError("删除失败");
		return true;
	}
	
}